package org.ant.gui.menu;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.ant.gui.actionListeners.CopyActionListener;
import org.ant.gui.actionListeners.CutActionListener;
import org.ant.gui.actionListeners.PasteActionListener;
import org.ant.gui.actionListeners.RedoActionListener;
import org.ant.gui.actionListeners.UndoActionListener;

public class EditMenu extends JMenu {
	
	public static final String NAME = "Edit";
	public static final String ITEM_UNDO = "Undo";
	public static final String ITEM_REDO = "Redo";
	public static final String ITEM_CUT = "Cut";
	public static final String ITEM_COPY = "Copy";
	public static final String ITEM_PASTE = "Paste";
	public static final String ITEM_SEARCH = "Search";
	
	private JMenuItem undoItem;
	private JMenuItem redoItem;
	private JMenuItem cutItem;
	private JMenuItem copyItem;
	private JMenuItem pasteItem;
	private JMenuItem searchItem;
	
	public EditMenu(){
		super();
		setText(NAME);
		
		add(getUndoItem());
		add(getRedoItem());
		
		addSeparator();
		
		add(getCutItem());
		add(getCopyItem());
		add(getPasteItem());
		
		addSeparator();
		
		add(getSearchItem());
	}

	public JMenuItem getUndoItem() {
		if(undoItem == null){
			undoItem = new JMenuItem();
			undoItem.setText(ITEM_UNDO);
			Icon icon = new ImageIcon("icon/undo.gif");
			undoItem.setIcon(icon);
			undoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
					InputEvent.CTRL_MASK));
			undoItem.addActionListener(new UndoActionListener());
		}
		return undoItem;
	}

	public JMenuItem getRedoItem() {
		if(redoItem == null){
			redoItem = new JMenuItem();
			redoItem.setText(ITEM_REDO);
			Icon icon = new ImageIcon("icon/redo.gif");
			redoItem.setIcon(icon);
			redoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,
					InputEvent.CTRL_MASK));
			redoItem.addActionListener(new RedoActionListener());
		}
		return redoItem;
	}

	public JMenuItem getCutItem() {
		if(cutItem == null){
			cutItem = new JMenuItem();
			cutItem.setText(ITEM_CUT);
			Icon icon = new ImageIcon("icon/cut.gif");
			cutItem.setIcon(icon);
			cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
					InputEvent.CTRL_MASK));
			cutItem.addActionListener(new CutActionListener());
		}
		return cutItem;
	}

	public JMenuItem getCopyItem() {
		if(copyItem == null){
			copyItem = new JMenuItem();
			copyItem.setText(ITEM_COPY);
			Icon icon = new ImageIcon("icon/copy.gif");
			copyItem.setIcon(icon);
			copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					InputEvent.CTRL_MASK));
			copyItem.addActionListener(new CopyActionListener());
		}
		return copyItem;
	}

	public JMenuItem getPasteItem() {
		if(pasteItem == null){
			pasteItem = new JMenuItem();
			pasteItem.setText(ITEM_PASTE);
			Icon icon = new ImageIcon("icon/paste.gif");
			pasteItem.setIcon(icon);
			pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					InputEvent.CTRL_MASK));
			pasteItem.addActionListener(new PasteActionListener());
		}
		return pasteItem;
	}

	public JMenuItem getSearchItem() {
		if(searchItem == null){
			searchItem = new JMenuItem();
			searchItem.setText(ITEM_SEARCH);
			Icon icon = new ImageIcon("icon/search.gif");
			searchItem.setIcon(icon);
			searchItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
					InputEvent.CTRL_MASK));
			searchItem.addActionListener(new RedoActionListener());
		}
		return searchItem;
	}
	
	
}
